class RankView extends egret.DisplayObjectContainer {
    private rankData: any;
    private personalRanks: any[];
    public constructor(data) {
        super();
        this.rankData = data;
        this.personalRanks = data['personal_rank'];
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.getViews, this);
        this.touchEnabled = true;
        this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.showRank, this);
    }
    private initGangRank() {
        if (this.rankData['gang_rank']) {
            if (this.rankData['gang_rank'].length > 0) {
                this.gangFirst = new RankTextField();
                this.gangFirst.text = utils.getChar(this.rankData['gang_rank'][0]['gang_name'], 15);
                this.gangFirst.x = 95;
                this.gangFirst.y = 155 + 25;
                this.addChild(this.gangFirst);
            }
            if (this.rankData['gang_rank'].length > 1) {
                this.gangSecond = new RankTextField();
                this.gangSecond.text = utils.getChar(this.rankData['gang_rank'][1]['gang_name'], 15);
                this.gangSecond.x = this.gangFirst.x;
                this.gangSecond.y = utils.getMaxY(this.gangFirst) + 15;
                this.addChild(this.gangSecond);
            }
            if (this.rankData['gang_rank'].length > 2) {
                this.gangThird = new RankTextField();
                this.gangThird.text = utils.getChar(this.rankData['gang_rank'][2]['gang_name'], 15);
                this.gangThird.x = this.gangFirst.x;
                this.gangThird.y = utils.getMaxY(this.gangSecond) + 15;
                this.addChild(this.gangThird);
            }
        }
    }
    private getViews() {
        let bg = utils.createBitmapByName('rank');
        bg.width = this.width;
        bg.height = this.height;
        this.addChild(bg);
        if (this.rankData['start_remain_time']) {
            return;
        }
        this.personalFirst = new RankTextField();
        if (this.personalRanks && this.personalRanks.length > 0) {
            this.personalFirst.text = utils.getChar(this.personalRanks[0]['nickname'], 15);
        } else {
            this.personalFirst.text = utils.getChar('', 15);
        }
        this.personalFirst.x = 95;
        this.personalFirst.y = 64;
        this.addChild(this.personalFirst);
        this.personalSecond = new RankTextField();
        if (this.personalRanks && this.personalRanks.length > 1) {
            this.personalSecond.text = utils.getChar(this.personalRanks[1]['nickname'], 15);
        } else {
            this.personalSecond.text = utils.getChar('', 15);
        }
        this.personalSecond.x = this.personalFirst.x;
        this.personalSecond.y = utils.getMaxY(this.personalFirst) + 15;
        this.addChild(this.personalSecond);
        this.personalThird = new RankTextField();
        if (this.personalRanks && this.personalRanks.length > 2) {
            this.personalThird.text = utils.getChar(this.personalRanks[2]['nickname'], 15);
        } else {
            this.personalThird.text = utils.getChar('', 15);
        }
        this.personalThird.x = this.personalFirst.x;
        this.personalThird.y = utils.getMaxY(this.personalSecond) + 15;
        this.addChild(this.personalThird);
        this.initGangRank();
    }

    private personalFirst: RankTextField;
    private personalSecond: RankTextField;
    private personalThird: RankTextField;
    private gangFirst: RankTextField;
    private gangSecond: RankTextField;
    private gangThird: RankTextField;
    private showRank() {
        if (GameData.hasBeginning) {
            Main.showJFView(GameData.lefTime);
        } else {
            Main.showLastJFView();
        }
    }

    private compare(property) {
        return function (a, b) {
            var value1 = a[property];
            var value2 = b[property];
            return value2 - value1;
        }
    }

    private removeDuplicatedItem(ar) {
        var ret = [];
        for (var i = 0, j = ar.length; i < j; i++) {
            let has = false;
            for (var k in ret) {
                if (ret[k]['tvmid'] == ar[i]['tvmid']) {
                    has = true;
                }
            }
            if (!has) {
                ret.push(ar[i]);
            }
        }
        return ret;
    }
    public refreshRank(myScore: number) {
        let tempRank = {};
        tempRank['tvmid'] = utils.getCookie('tvmid');
        tempRank['nickname'] = user_info['nickname'];
        tempRank['total_score'] = myScore;
        this.personalRanks.push(tempRank);
        this.personalRanks.sort(this.compare('total_score'));//数组首先排序
        this.personalRanks = this.removeDuplicatedItem(this.personalRanks);//数组去重
        if (this.personalRanks.length > 0) {
            this.personalFirst.text = utils.getChar(this.personalRanks[0]['nickname'], 15);
        }
        if (this.personalRanks.length > 1) {
            this.personalSecond.text = utils.getChar(this.personalRanks[1]['nickname'], 15);
        }
        if (this.personalRanks.length > 2) {
            this.personalThird.text = utils.getChar(this.personalRanks[2]['nickname'], 15);
        }
    }
}

class RankTextField extends BaseTextField {
    constructor() {
        super();
        this.fontFamily = '微软雅黑';
        this.size = 20;
        this.textColor = 0xffea22;
        this.stroke = 2;
        this.strokeColor = 0x5a3b0f;
    }
}